package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class StoreRepositoryImpl implements StoreRepository {
    private final StoreMapper storeMapper;

    @Override
    public Page<StorePO> getStorePageList(StoreQuery storeQuery, Page<StorePO> page) {
        // 创建查询条件
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(storeQuery.getBusinessId() != null, "business_id", storeQuery.getBusinessId());
        queryWrapper.like(storeQuery.getStoreName() != null, "store_name", storeQuery.getStoreName());
        queryWrapper.eq(storeQuery.getStoreHeadPhone() != null, "store_head_phone", storeQuery.getStoreHeadPhone());
        queryWrapper.eq(storeQuery.getProvinceId() != null, "province_id", storeQuery.getProvinceId());
        queryWrapper.eq(storeQuery.getAreaId() != null, "area_id", storeQuery.getAreaId());
        queryWrapper.eq(storeQuery.getCityId() != null, "city_id", storeQuery.getCityId());
        queryWrapper.ge(storeQuery.getStartingTime() != null, "update_time", storeQuery.getStartingTime());
        queryWrapper.le(storeQuery.getEndTime() != null, "update_time", storeQuery.getEndTime());
        queryWrapper.eq(storeQuery.getStoreStatus() != null, "store_status", storeQuery.getStoreStatus());

        // 执行分页查询
        return storeMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void save(StorePO po) {
        storeMapper.insert(po);
    }

    @Override
    public Page<StorePO> getStoreByCityAndBusinessId(List<Long> cityIdList, Integer businessId, Page<StorePO> page) {
        LambdaQueryWrapper<StorePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StorePO::getBusinessId, businessId);
        if (cityIdList != null && !cityIdList.isEmpty()) {
            queryWrapper.in(StorePO::getCityId, cityIdList);
        }
        return storeMapper.selectPage(page, queryWrapper);
    }
}
